//
//  ListMiViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/6/27.
//

#import "ListMiViewController.h"
#import "ListPeopleCollectionViewCell.h"
//#import <ZJKitTool/ZJKitTool.h>
#import "ZJVerticalFlowLayout.h"
#import "PeopleDetaileModel.h"
@interface ListMiViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,ZJVerticalFlowLayoutDelegate>

@end

@implementation ListMiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)SQ_UIViewForSelfView{
    ZJVerticalFlowLayout *layout = [[ZJVerticalFlowLayout alloc]initWithDelegate:self];
    self.collectionView.collectionViewLayout = layout;
    [self.collectionView registerClass:[ListPeopleCollectionViewCell class] forCellWithReuseIdentifier:@"ListPeopleCollectionViewCell"];
    [self.view addSubview:self.collectionView];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).insets(UIEdgeInsetsMake(TopHeight, 0, 50+FootHeight, 0));
    }];
    [self SQ_collectViewAddRefresh];
    [self SQ_Header];
}

- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"1" forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [dic setObject:@(self.topModel.type) forKey:@"type"];
    UserModel * model =  [UserModel share];
    if (model.lat > 0 ) {
        [dic setObject:[NSString stringWithFormat:@"%.6f",model.lat] forKey:@"lat"];
        [dic setObject:[NSString stringWithFormat:@"%.6f",model.lng] forKey:@"lng"];
    }
    [HttpTool post:Url57 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page = 1;
        [self.collectionView.mj_header endRefreshing];
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [PeopleDetaileModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray removeAllObjects];
        [self.jsonArray addObjectsFromArray:array];
        [self.collectionView reloadData];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.collectionView.mj_header endRefreshing];
    }];
}
- (void)SQ_Foother{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSString stringWithFormat:@"%d",self.page+1] forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [dic setObject:@(self.topModel.type) forKey:@"type"];
    UserModel * model =  [UserModel share];
    if (model.lat > 0 ) {
        [dic setObject:[NSString stringWithFormat:@"%.6f",model.lat] forKey:@"lat"];
        [dic setObject:[NSString stringWithFormat:@"%.6f",model.lng] forKey:@"lng"];
    }
    [HttpTool post:Url57 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page ++;
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [PeopleDetaileModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray addObjectsFromArray:array];
        [self.collectionView reloadData];
        [self.collectionView.mj_footer endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.collectionView.mj_footer endRefreshing];
    }];
}


#pragma mark UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.jsonArray.count;
    
}

-(UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    ListPeopleCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ListPeopleCollectionViewCell" forIndexPath:indexPath];
    PeopleDetaileModel * model = [self.jsonArray objectAtIndex:indexPath.item];
    cell.model = model;
    return cell;
}

#pragma mark - ZJVerticalFlowLayoutDelegate

/**
 * 需要显示的列数, 默认3
 */
- (NSInteger)zj_waterflowLayout:(ZJVerticalFlowLayout *)waterflowLayout columnsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}



/**
 * 设置cell的高度

 @param indexPath 索引
 @param itemWidth 宽度
 */
-(CGFloat)zj_waterflowLayout:(ZJVerticalFlowLayout *)waterflowLayout collectionView:(UICollectionView *)collectionView heightForItemAtIndexPath:(NSIndexPath *)indexPath itemWidth:(CGFloat)itemWidth{
    PeopleDetaileModel * model = [self.jsonArray objectAtIndex:indexPath.item];
    return model.cellSize.height;
}

/**
 * 列间距
 */
-(CGFloat)zj_waterflowLayout:(ZJVerticalFlowLayout *)waterflowLayout columnsMarginInCollectionView:(UICollectionView *)collectionView{
    return 10;
}


/**
 * 行间距, 默认10
 */
- (CGFloat)zj_waterflowLayout:(ZJVerticalFlowLayout *)waterflowLayout collectionView:(UICollectionView *)collectionView linesMarginForItemAtIndexPath:(NSIndexPath *)indexPath{
    return 10;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    PeopleDetailViewController * vc = [[PeopleDetailViewController alloc]init];
    PeopleDetaileModel * model = [self.jsonArray objectAtIndex:indexPath.item];
    vc.uid = model.userid;
    [self SQ_pushViewControllerFull:vc];
}

 

@end
